﻿//======================================================================
//
//        Copyright (C) 2015   
//        All rights reserved
//
//        filename : CP_ReturnMonthSta
//        description : 成品退换卡片统计月报表
//        created by XXX at  20160504
//
//======================================================================
using System;
using System.Data;
using CardLogistics.Web.Components;
using CardLogistics.DAL;
using System.Text;

namespace CardLogistics.Web.MainPage.Report.DepReport
{
    public partial class CP_ReturnMonthSta : BasePage
    {
        private static readonly DepReportData DepDAL = new DepReportData();

        #region 查询
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Search_Click(object sender, EventArgs e)
        {
            if (txt_Data.Text.Trim().Length > 0)
            {
                string startV = txt_Data.Text + "-01";//统计月份的第一天
                string endV = DateTime.Parse(startV).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");//统计月份的最后一天
                BindData(startV, endV);
            }
            else
                ShowMessageBox("请选择统计月份！");
                txt_Data.Focus();
        }
        #endregion

        #region 绑定数据
        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="startData"></param>
        /// <param name="endData"></param>
        private void BindData(string startData, string endData)
        {
            DataTable dt = DepDAL.GetCPReturnMonthSta(startData, endData);
            StringBuilder sbStr = new StringBuilder();
            StringBuilder sbCardType = new StringBuilder("<tr><td colspan='2' rowspan='2'></td>");//卡类型
            StringBuilder sbSL = new StringBuilder("<tr>");//数量
            StringBuilder sbSQJC = new StringBuilder("<tr><td colspan='2'>上期结存</td>");//上期结存
            StringBuilder sbBQRK = new StringBuilder("<tr><td colspan='2'>本期入库</td>");//本期入库
            StringBuilder sbBF = new StringBuilder("<tr><td colspan='2'>报废</td>");//报废
            StringBuilder sbXHCK = new StringBuilder("<tr><td colspan='2'>销毁出库</td>");//销毁出库
            StringBuilder sbBQJC = new StringBuilder("<tr><td colspan='2'>本期结存</td>");//本期结存
            sbStr.Append("<div style='text-align: center; font-size: 25px; font-weight: bold'>西安城市一卡通有限责任公司退换卡片统计月报表</div>");
            sbStr.Append("<table  style='border:0;width:100%;'><tr><td colspan='1' style='border:0;text-align:right;'>填报单位:</td><td colspan='4' style='border:0;text-align:center;'>结算日期:" + DateTime.Parse(startData).ToString("yyyyMMdd") + "-" + DateTime.Parse(endData).ToString("yyyyMMdd") + "</td><td  colspan='" + (dt.Rows.Count - 6) + "' style='border:0;text-align:right;'>填表日期:" + DateTime.Now.ToString("yyyy-MM-dd") + "</td></tr></table>");
            sbStr.Append("<table  border='1' style='font-size:16px;white-space: nowrap; '>");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                sbCardType.Append("<td style='width:70px'>" + dt.Rows[i]["cardtypename"].ToString() + "</td>");
                sbSL.Append("<td>数量(张)</td>");
                sbSQJC.Append("<td>" + dt.Rows[i]["上期结存"].ToString() + "</td>");
                sbBQRK.Append("<td>" + dt.Rows[i]["本期入库"].ToString() + "</td>");
                sbBF.Append("<td>" + dt.Rows[i]["报废"].ToString() + "</td>");
                sbXHCK.Append("<td>" + dt.Rows[i]["销毁出库"].ToString() + "</td>");
                sbBQJC.Append("<td>" + dt.Rows[i]["本期结存"].ToString() + "</td>");
            }
            sbCardType.Append("<td>合计</td><td rowspan='2'>备 注</td></tr>");
            sbSL.Append("<td>数量(张)</td></tr>");
            sbSQJC.Append("<td>" + dt.Compute("sum(上期结存)", "true").ToString() + "</td><td></td></tr>");
            sbBQRK.Append("<td>" + dt.Compute("sum(本期入库)", "true").ToString() + "</td><td></td></tr>");
            sbBF.Append("<td>" + dt.Compute("sum(报废)", "true").ToString() + "</td><td></td></tr>");
            sbXHCK.Append("<td>" + dt.Compute("sum(销毁出库)", "true").ToString() + "</td><td></td></tr>");
            sbBQJC.Append("<td>" + dt.Compute("sum(本期结存)", "true").ToString() + "</td><td></td></tr>");
            sbStr.Append(sbCardType).Append(sbSL).Append(sbSQJC).Append(sbBQRK).Append(sbBF).Append(sbXHCK).Append(sbBQJC);

            d_info.InnerHtml = sbStr.ToString() + "</table>";
        }
        #endregion

        #region 导出事件
        /// <summary>
        /// 导出事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            if (d_info.InnerHtml.Length > 50)
            {
                ExportExcel(txt_Data.Text, d_info);
            }
            else
                ShowMessageBox("请先查询出报表内容！");
        }
        #endregion
    }
}